﻿using Kingdee.BOS.Core.Attachment;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Core.Metadata.FieldElement;
using System.ComponentModel;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Orm.DataEntity;

namespace NS
{
    [HotUpdate]
    [Description("采购订单点击菜单附件查看按钮查询关联付款单附件信息")]
    public class ViewAttachments : AbstractListPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.EqualsIgnoreCase("tbBtnShowAttachments"))
            {
                ListSelectedRowCollection collection = this.ListView.SelectedRowsInfo;
                if(collection.Count>1)
                {
                    this.View.ShowErrMessage("只能选择单条数据进行附件操作！");
                    return;
                }
                string fid = "";
                string SQL = "/*dialect*/" + string.Format(@"select t1.fid from t_PUR_POOrder t
                left join T_AP_PAYBILLSRCENTRY t1 on t.fbillno=t1.FPURORDERNO
                where t.fid='{0}'", this.ListView.CurrentSelectedRowInfo.PrimaryKeyValue);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL);
                if(DynamData.Count>0)
                {
                    fid = Convert.ToString(DynamData[0]["fid"]);
                }
                if(fid.Equals("0"))
                {
                    this.View.ShowErrMessage("未下推付款单！");
                    return;
                }
                AttachmentKey attaKey = new AttachmentKey
                {
                    OperationStatus = this.View.OpenParameter.Status, //附件列表操作状态。
                    BillType = "AP_PAYBILL", //单据业务对象标识。
                    //BillInterID = this.View.Model.GetPKValue().GetString(), //单据内码。
                    BillInterID = fid,
                    EntryKey = " ", //单据体标识。
                    EntryInterID = "-1", //单据体内码。
                    RowIndex = 0 //行下标。
                };
                //Field billNoField = this.View.BusinessInfo.GetBillNoField();

                //if (billNoField != null)
                //{
                //    attaKey.BillNo = this.View.Model.GetValue(billNoField.Key).GetString(); //单据编号。
                //}

              

                ListShowParameter param = new ListShowParameter();
                param.FormId = FormIdConst.BOS_Attachment;
                param.Caption = "附件列表";
                param.OpenStyle.ShowType = ShowType.Default;
                param.Width = 800;
                param.Height = 500;
                param.MultiSelect = true;
                param.IsLookUp = false;
                param.IsShowQuickFilter = false;
                param.CustomParams.Add(KeyConst.AttachmentKey, AttachmentKey.ConvertToString(attaKey));
                //附件列表过滤条件，此处过滤出当前单据的单据头单据体附件，用户可根据需要过滤出自己想要看到的附件数据。
                param.ListFilterParameter.Filter = string.Format("FBillType = '{0}' AND FInterId = '{1}'",
                    attaKey.BillType, attaKey.BillInterID);
                this.View.ShowForm(param);

            }
        }
    }

    //private void ShowAttachments()
    //{
    //    AttachmentKey attaKey = new AttachmentKey
    //    {
    //        OperationStatus = this.View.OpenParameter.Status, //附件列表操作状态。
    //        BillType = this.View.BusinessInfo.GetForm().Id, //单据业务对象标识。
    //        BillInterID = this.View.Model.GetPKValue().GetString(), //单据内码。
    //        EntryKey = " ", //单据体标识。
    //        EntryInterID = "-1", //单据体内码。
    //        RowIndex = 0 //行下标。
    //    };
    //    Field billNoField = this.View.BusinessInfo.GetBillNoField();
    //    if (billNoField != null)
    //    {
    //        attaKey.BillNo = this.View.Model.GetValue(billNoField.Key).GetString(); //单据编号。
    //    }

    //    ListShowParameter param = new ListShowParameter();
    //    param.FormId = FormIdConst.BOS_Attachment;
    //    param.Caption = "附件列表";
    //    param.OpenStyle.ShowType = ShowType.Default;
    //    param.Width = 800;
    //    param.Height = 500;
    //    param.MultiSelect = true;
    //    param.IsLookUp = false;
    //    param.IsShowQuickFilter = false;
    //    param.CustomParams.Add(KeyConst.AttachmentKey, AttachmentKey.ConvertToString(attaKey));
    //    //附件列表过滤条件，此处过滤出当前单据的单据头单据体附件，用户可根据需要过滤出自己想要看到的附件数据。
    //    param.ListFilterParameter.Filter = string.Format("FBillType = '{0}' AND FInterId = '{1}'",
    //        attaKey.BillType, attaKey.BillInterID);
    //    this.View.ShowForm(param);
    //}
}
